package AGV;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: 蔡翔
 * @Date: 2019/7/19 13:26
 * @Version 1.0
 */

//（终点） rabbitmq 有一个好处，当你 exchanges 和 queues 不存在的时候，你只要new 一个 Consumer 调用就可以了。因为如果不存在它会帮你创建。
public class Consumer {
    public static void main(String[] args) throws Exception {
        Logger logger = LoggerFactory.getLogger(Consumer.class);

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.0.229");
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPort(5672);

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        String exchange = "AGVC_exchanges";
        String routingKey = "AGVC.direct";
        String queueName = "AGVC_queues";
        String type = "direct";

        channel.queueDeclare(queueName,true,false,false,null);
        channel.exchangeDeclare(exchange,type,true);
        channel.queueBind(queueName,exchange,routingKey);

        //后面要用这个，因为要把这个 Consumer当成一个Runnable 丢到线程池，而java 不支持 多重继承。
        /*DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);
                String msg = new String(body,"UTF-8");
                System.out.println("客戶端接收到的msg是："+msg);
            }
        };*/

        channel.basicConsume(queueName,false,new MyConsumer(channel));
    }
}
